﻿using Microsoft.AspNetCore.Mvc;
using ZhCun.WebCommon.AL;
using ZhCun.WebCommon.ApiModels;
using ZhCun.WebCommon.DBModels;
using ZhCun.WebCommon.ViewModels;

namespace ZhCun.WebCommon.WebApi
{
    public class ApiUserBaseController<TALUser> : ApiAuthorizeController<TALUser>
        where TALUser : ALUserBase, new()
    {
        /// <summary>
        /// 登录
        /// </summary>
        [NonAction]
        protected virtual ApiResult Login(UserBaseArgLogin arg)
        {
            if (arg == null)
            {
                return CreateResult(false, "参数有误");
            }
            //登录模式 1：webapi 2:web page
            var tokenTimeout = AppCommonConfig.AppSettings.TokenTimeOutByApi;
            if (ALObj.Login(arg.LoginName, arg.LoginPwd, tokenTimeout, 1, arg.ClientSN, out string msg))
            {
                UserBaseRetLogin ret = new UserBaseRetLogin();
                ret.UserName = ALObj.LoginUser.UserName;
                ret.Token = ALObj.LoginUser.LoginToken;
                ret.ExpiresTime = ALObj.LoginUser.LoginExpiresTime;
                return CreateResult(ret);
            }
            else
            {
                return CreateResult(false, msg);
            }
        }
        /// <summary>
        /// token登录，重新登录时通过token，失败时请使用用户名登录，ClientSN不一致验证不通过
        /// </summary>        
        [HttpPost]
        public ApiResult TokenLogin()
        {
            var tokenTimeout = AppCommonConfig.AppSettings.TokenTimeOutByApi;

            if (ALObj.Login(LoginToken.LoginToken, tokenTimeout, 1, out string msg))
            {
                UserBaseRetLogin ret = new UserBaseRetLogin();
                ret.UserName = ALObj.LoginUser.UserName;
                ret.Token = ALObj.LoginUser.LoginToken;
                ret.ExpiresTime = ALObj.LoginUser.LoginExpiresTime;
                return CreateResult(ret);
            }
            else
            {
                return CreateResult(false, msg);
            }
        }

        /// <summary>
        /// 用户信息保存
        /// </summary>
        [NonRoleVerify]
        [HttpPost]
        public ApiResult InfoSave(UserBaseArgInfo arg)
        {
            TSysUser user = new TSysUser();
            user.UserName = arg.UserName;
            user.MobileNumber = arg.MobileNumber;
            var r = ALObj.ChangeUserInfo(user, out string msg);
            return CreateResult(r, msg);
        }
        /// <summary>
        /// 修改密码保存
        /// </summary>
        [NonRoleVerify]
        [HttpPost]
        public ApiResult PwdSave(UserBaseArgPwd arg)
        {
            if (arg.NewPwd1 != arg.NewPwd2)
            {
                return CreateResult(false, "两次新密码不一致");
            }
            bool r = ALObj.ChangePassword(arg.OldPwd, arg.NewPwd1, out string errMsg);
            if (r)
            {
                ALObj.WriteLog("修改密码");
            }
            return CreateResult(true, errMsg);
        }
        ///// <summary>
        ///// 修改密码（非action）
        ///// </summary>        
        //[NonAction]
        //public ApiResult ChangePassword(UserBaseArgPwd arg)
        //{
        //    if (arg.NewPwd1 != arg.NewPwd2)
        //    {
        //        return CreateResult(false, "两次新密码不一致");
        //    }
        //    bool r = ALObj.ChangePassword(arg.OldPwd, arg.NewPwd1, out string errMsg);
        //    if (r)
        //    {
        //        ALObj.WriteLog("修改密码");
        //    }
        //    return CreateResult(true, errMsg);
        //}
        ///// <summary>
        ///// 修改用户信息，不包含密码，非action
        ///// </summary>        
        //[NonAction]
        //public ApiResult ChangeUserInfo(UserBaseArgInfo arg)
        //{
        //    TSysUser user = new TSysUser();
        //    user.UserName = arg.UserName;
        //    user.MobileNumber = arg.MobileNumber;
        //    //if (string.IsNullOrWhiteSpace(user.LoginName))
        //    //{
        //    //    user.LoginName = arg.MobileNumber;
        //    //}            
        //    var r = ALObj.ChangeUserInfo(user, out string msg);
        //    return CreateResult(r, msg);
        //}
        /// <summary>
        /// 用户登录退出
        /// </summary>
        [HttpPost]
        [NonAction]
        public ApiResult LoginOut()
        {
            LoginStateCache.Remove(LoginToken.LoginToken);
            var r = ALObj.LoginOut();
            if (r)
            {
                ALObj.WriteLog("用户退出登录");
                return CreateResult(true, "退出成功");
            }
            else
            {
                return CreateResult(false, "退出失败");
            }
        }
    }
}